#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
int main()
{
    /*
     *1、创建套接字
     *2、发送消息
     *3、接收消息
     * */
    int udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(udp_fd < 0)
    {
        perror("socket");
        return 0;
    }
    //发送消息
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(22222);
    addr.sin_addr.s_addr = inet_addr("10.0.24.13");

    const char* str = "Hello, I am udp_client!";
    ssize_t send_size = sendto(udp_fd, str, strlen(str), 0 ,(struct sockaddr*)&addr, sizeof(addr));
    if(send_size < 0)
    {
        perror("sendto");
        return 0;
    }
    //接收消息
    char buf[1024] = {0};
    struct sockaddr_in serve_addr;
    socklen_t serve_addrlen = sizeof(serve_addr);
    ssize_t rcv_size = recvfrom(udp_fd, buf, sizeof(buf) - 1, 0, (struct sockaddr*)&serve_addr, &serve_addrlen);
    if(rcv_size < 0){
        return 0;
    }
    printf("%s : %d send value is : %s\n", inet_ntoa(serve_addr.sin_addr), ntohs(serve_addr.sin_port), buf);
    close(udp_fd);
    return 0;
 }

